import { DataSource } from "./DataSource"
import { TransactionContext } from "./TransactionContext"
import { ConnectionInfo } from "./ConnectionInfo"

export class TransactionManager {
  constructor(private dataSource: DataSource) {
    TransactionContext.getInstance().setTransactionManager(this)
  }

  async getConnectionInfo(): Promise<ConnectionInfo> {
    const transactionInfo = TransactionContext.getInstance().getConnectionInfo()
    if (transactionInfo != null) {
      // console.log('getConnection from transactionInfo')
      return transactionInfo
    } else {
      return new ConnectionInfo(await this.dataSource.getConnection())
    }
  }

  getDatasource(): DataSource {
    return this.dataSource
  }
}
